<?php
namespace Mentees\Controller;

class MessageController extends MainController {

    public function index(){
        $obj = M('Message');
        $map['toID']=$this->userID;
        $map['toDel']=0;
        $map['type']=1;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display("index");
    }   

    public function send(){
        $toID = I('get.toID');
        $toUser = M('Mentees')->where('userid='.$toID)->find();
        $fromUser = M('Mentors')->where('userid='.$this->userID)->find();
        $this->assign('fromUser',$fromUser);
        $this->assign('toUser',$toUser);
        $this->display();
    }

    public function save(){
        $this->all_add('Message');
    }

    public function view(){
        $id = (int)I('get.id');
        $obj = M('Message');
        $map['toID']=$this->userID;
        $map['id']=$id;
        $list = $obj->where($map)->find();

        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {
            $obj->where($map)->setField('isread',1);
            $this->assign('list',$list);
            $this->display();
        }
    }

    //发件箱
    public function sendbox(){
        $obj = M('Message');
        $map['fromID']=$this->userID;
        $map['fromDel']=0;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function sendview(){
        $id = (int)I('get.id');
        $obj = M('Message');
        $map['fromID']=$this->userID;
        $map['id']=$id;
        $list = $obj->where($map)->find();

        if (!$list) {
            $this->error(L('_public_noinfo'));
        } else {   
            $this->assign('list',$list);
            $this->display();
        }
    }

    public function del(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['toID'] = $this->userID;    
        $obj = M('Message');
        $obj->where($map)->setField('toDel',1);
        redirect(U('Message/index'));
    }

    public function delsend(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['fromID'] = $this->userID;    
        $obj = M('Message');
        $obj->where($map)->setField('fromDel',1);
        redirect(U('Message/sendbox'));
    }

    //系统消息
    public function system(){
        $obj = M('Message');
        $map['toID']=$this->userID;
        $map['type']=0;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    } 

    public function truedel(){
        $id = I('id');
        $map['type'] = 0;
        $map['toID'] = $this->userID;    
        $obj = M('Message');
        if(is_array($id)){
            foreach ($id as $v) {
                $map['id'] = $v;
                $obj->where($map)->delete();
            }
        }else{
           $map['id'] = $id;
           $obj->where($map)->delete(); 
        }
        
        redirect(U('Message/system'));
    }

    public function alldel(){
        $id=I('post.selectedids');
        $type=I("type");
        if($id==''){
             echo $this->echo_json_str("ERROR",L('_no_selected'));exit;
        }else{
            if($type=="inbox"){
                $where['toID']=$this->userID;
                $url=U('Message/index');
            }else if($type=="send"){
                $where['fromID']=$this->userID;
                $url=U('Message/sendbox');
            }else{
                echo $this->echo_json_str("ERROR",L('_sys_error'));exit; 
            }
            foreach ($id as $v) {
                $obj = M('Message');
                $where['id'] = $v;
                if($type=="inbox"){
                   $obj->where($where)->setField('toDel',1);
                }else if($type=="send"){
                   $obj->where($where)->setField('fromDel',1);
                }
            }
            echo $this->echo_json_str("SUCCESS",L('_action_success'),$url);exit;
        }
    }

}